/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-08-23
 * Time: 20:10
 */
public class MyBlockingQueue {
    private String[] array;
    private int head;
    private int tail;
    private int size;
    private Object locker = new Object();
    public MyBlockingQueue(int capacity){
        if(capacity <= 0){
            throw new IllegalArgumentException("capacity must be positive.");
        }
        array = new String[capacity];
    }
    public void put(String elem) throws InterruptedException {
        synchronized (locker){
            while(size == array.length){
                locker.wait();
            }
            array[tail] = elem;
            tail++;
            if(tail >= array.length){
                tail = 0;
            }
            size++;
            notify();
        }
    }
    public String take() throws InterruptedException {
        synchronized (locker){
            while(size == 0){
                locker.wait();
            }
            String ret = array[head];
            head++;
            if(head >= array.length){
                head = 0;
            }
            size--;
            notify();
            return ret;
        }

    }
}
